package com.zfast.spring5.junit;


import lombok.extern.slf4j.Slf4j;
import org.activiti.engine.*;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zgn
 * @Description: simJunit
 * @date 2022/1/13 0013
 */
@Slf4j
@SpringBootTest
public class Activiti7Junit {
    @Resource
    RepositoryService repositoryService;
    @Resource
    RuntimeService runtimeService;
    @Resource
    TaskService taskService;

    @Test
    public void egMin() {//e.g.
        System.out.println();
    }

    @Test
    public void 初始化数据库_egMin() {//e.g.
        ProcessEngineConfiguration.createProcessEngineConfigurationFromResourceDefault().setDatabaseSchemaUpdate("create").buildProcessEngine();
        System.out.println();
    }

    @Test
    public void 初始化数据库_java() {
        // 引擎配置
        ProcessEngineConfiguration pec=ProcessEngineConfiguration.createStandaloneProcessEngineConfiguration();
        pec.setJdbcDriver("com.mysql.jdbc.Driver");
        pec.setJdbcUrl("jdbc:mysql://localhost:3306/db_activiti?nullCatalogMeansCurrent=true");//?nullCatalogMeansCurrent=true
        pec.setJdbcUsername("root");
        pec.setJdbcPassword("0000");

        /**
         * false 不能自动创建表
         * create-drop 先删除表再创建表
         * true 自动创建和更新表
         */
        pec.setDatabaseSchemaUpdate(ProcessEngineConfiguration.DB_SCHEMA_UPDATE_TRUE);

        // 获取流程引擎对象
        ProcessEngine processEngine=pec.buildProcessEngine();
    }

    @Test
    public void 初始化数据库_WithXml(){
        // 引擎配置
        ProcessEngineConfiguration pec=ProcessEngineConfiguration.createProcessEngineConfigurationFromResource("activiti.cfg.xml");
        // 获取流程引擎对象
        ProcessEngine processEngine=pec.buildProcessEngine();
    }

    @Test
    public void testDeployment(){
        Deployment deployment = repositoryService.createDeployment().name("测试流程文件部署")
                .addClasspathResource("test001.bpmn20.xml")//test002.bpmn;
                //.addClasspathResource("test.png")
                .deploy();
        log.info("部署id为："+deployment.getId());
        log.info("部署名称为："+deployment.getName());
    }

    //获取部署的流程
    @Test
    public void getDeployment() {
        List<Deployment> deploymentList = repositoryService.createDeploymentQuery().list();

        if (!CollectionUtils.isEmpty(deploymentList)) {
            deploymentList.stream().forEach(item -> {
                log.info("部署id为:" + item.getId());
                log.info("部署名称为:" + item.getName());
            });
        }
    }

    @Test
    public void testStartProcess(){
        String instanceKey = "test001";
        log.info("开启流程...");
        Map<String, Object> map = new HashMap<String, Object>();
        ProcessInstance instance = runtimeService.startProcessInstanceByKey(instanceKey, map);
        log.info("启动流程实例成功:{}", instance);
        log.info("流程实例ID:{}", instance.getId());
        log.info("流程定义ID:{}", instance.getProcessDefinitionId());
    }

    //根据指定的任务办理人查询任务并办理
    @Test
    public void testGetTaskByAssignee(){
        List<Task> tasks = taskService.createTaskQuery().taskAssignee("1002Assign").list();
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("fullMsg", "家中有事请假5天");
        for (org.activiti.engine.task.Task task : tasks) {
            log.info("任务id为=="+task.getId());
            taskService.complete(task.getId(),map);
        }
    }

    @Test
    public void egMax() {//e.g.
        //region 1.模拟数据
        System.out.println("--------------------Start--------------------");
        //endregion

        //region 2.数据处理
        System.out.println("--------------------Deal--------------------");
        //endregion

        //region 3.数据展示
        System.out.println("--------------------Done--------------------");
        //endregion
    }
}
